package com.cloudspace.jindun.adapter.locate;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cloudspace.jindun.JindunApp;
import com.cloudspace.jindun.R;
import com.cloudspace.jindun.activity.locate.LocateAreaPersonsActivity;
import com.cloudspace.jindun.model.LocateArea;
import com.cloudspace.jindun.utils.ObjUtil;

import java.util.List;

/**
 * Created by zengxianhua on 17/3/4.
 */

public class LocateAreaAdapter extends BaseAdapter{
    private List<LocateArea> datas;
    private Activity mContext;

    private View.OnClickListener detailListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            LocateArea data = (LocateArea) v.getTag();
            LocateAreaPersonsActivity.showActivity(mContext, data, departStatistics ? data.dempartName : "");
        }
    };

    public void setDatas(List<LocateArea> datas, Activity context){
        this.datas = datas;
        mContext = context;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (ObjUtil.isEmpty(datas)){
            return 0;
        }

        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        if (ObjUtil.isEmpty(datas)){
            return null;
        }

        if (position < 0 || position >= datas.size()){
            return null;
        }

        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        if (convertView == null){
            convertView = View.inflate(JindunApp.getInstance(), R.layout.locate_statistics_item, null);

            holder = new ViewHolder();
            holder.areaTv = (TextView) convertView.findViewById(R.id.area_tv);
            holder.personNumberTv = (TextView) convertView.findViewById(R.id.number_tv);
            holder.safeLabelTv = (TextView) convertView.findViewById(R.id.safe_tv);
            holder.detailView = convertView.findViewById(R.id.detail_iv);

            convertView.setTag(holder);
        }

        holder = (ViewHolder) convertView.getTag();
        LocateArea data = datas.get(position);
        holder.safeLabelTv.setText(data.areaSafeLevel);
        holder.personNumberTv.setText(String.valueOf(data.total));

        if (departStatistics){
            holder.areaTv.setText(data.dempartName);
            holder.safeLabelTv.setVisibility(View.GONE);
        }else{
            holder.areaTv.setText(data.areaName);
        }

        holder.detailView.setTag(data);
        holder.detailView.setOnClickListener(detailListener);

        return convertView;
    }

    private boolean departStatistics = false;

    public void setType(boolean departStatistics) {
        this.departStatistics = departStatistics;
    }

    static class ViewHolder{
        TextView areaTv;
        TextView personNumberTv;
        TextView safeLabelTv;
        View detailView;
    }
}


